package cake.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cake.dao.UserDao;
import cake.dao.impl.UserDaoImpl;
import cake.entity.User;
@WebServlet("/login.html")

public class LoginServlet extends HttpServlet{
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		if (req.getParameter("out")!= null) {
			req.getSession().removeAttribute("LoginUser");;
		}
		req.getRequestDispatcher("login.jsp").forward(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		UserDao userDao=new UserDaoImpl();
		String userName=req.getParameter("username");
		String passWord=req.getParameter("password");
		User user=new User();
		user=userDao.checkLoginUser(userName, passWord);
		if (user!= null && user.isStatus()) {
			req.getSession().setAttribute("LoginUser", user);
			resp.sendRedirect("index.html");
		} else {
			req.setAttribute("error", "用户名或密码错误");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}
	}
}
